import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/components/health/main_header.dart';
import 'package:flutter_eve_plugin/components/health/radar_chart.dart';
import 'package:flutter_eve_plugin/components/health/sys_describe_view.dart';
import 'package:flutter_eve_plugin/components/health/sys_state_view.dart';
import 'package:flutter_eve_plugin/components/main/app_bar.dart';
import 'package:flutter_eve_plugin/model/health/sys_describe.dart';
import 'package:flutter_eve_plugin/model/health/sys_health_property.dart';
import 'package:flutter_eve_plugin/pages/health/actions.dart';
import 'package:flutter_eve_plugin/route/main.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';
import 'package:flutter_redux/flutter_redux.dart';

class HealthMainPage extends StatefulWidget {
  final String title;

  HealthMainPage({Key key, this.title}) : super(key: key);

  @override
  _HealthMainPageState createState() => _HealthMainPageState();
}

class _HealthMainPageState extends State<HealthMainPage> {
  @override
  Widget build(BuildContext context) {
    List<SysDescribeModel> descList = [
      SysDescribeModel(
          title: '消化系统:',
          desc: '是琥珀营养的唯一来源，建议保持良好的饮食习惯，密切关注琥珀的饥饿值。',
          link: RouteConstant.SysStateMainPage,
          index: 1),
      SysDescribeModel(
          title: '神经系统:',
          desc: '对生理功能调节起到重要作用，需要保持良好睡眠习惯和体力值。',
          link: RouteConstant.SysStateMainPage,
          index: 3),
      SysDescribeModel(
          title: '运动系统:',
          desc: '琥珀的日常行动的重要系统，建议保持健康的体重范围。',
          link: RouteConstant.SysStateMainPage,
          index: 2),
      SysDescribeModel(
          title: '呼吸系统:',
          desc: '琥珀赖以生存的重要系统，需要关注营养值，避免抵抗力下降。',
          link: RouteConstant.SysStateMainPage,
          index: 0),
      SysDescribeModel(
          title: '内分泌系统:',
          desc: '影响琥珀新陈代谢的重要系统，建议关注卡路里的摄取量，保持健康的营养摄入。',
          link: RouteConstant.SysStateMainPage,
          index: 4),
    ];

    return StoreConnector<AppState, AppStateViewModel>(
        onInit: (store) {
          store.dispatch(initData());
        },
        converter: (store) => AppStateViewModel(
              state: store.state,
            ),
        builder: (context, vm) {
          List<RadarViewModel> dataList = [
            buildDataListItem(vm, '神经系统'),
            buildDataListItem(vm, '内分泌系统'),
            buildDataListItem(vm, '呼吸系统'),
            buildDataListItem(vm, '运动系统'),
            buildDataListItem(vm, '消化系统'),
          ];

          return Scaffold(
            appBar: buildAppBar(context, title: widget.title),
            body: CustomScrollView(
                physics: BouncingScrollPhysics(),
                slivers: <Widget>[
                  HealthMainHeaderWidget(),
                  SysStateWidget(
                      dataList: dataList,
                      userColor: userColor(
                          vm.state.headlthState.propertyHealthModel.illStatus)),
                  SysDescribeWidget(descList: descList),
                ]),
          );
        });
  }

  ///只要有一个系统生病，那个系统健康值就会显示为0, 显示疾病值
  RadarViewModel buildDataListItem(AppStateViewModel vm, String key) {
    var list = vm.state.headlthState.propertyHealthModel.propertyHealthData
        ?.where((item) {
      return item.healthSystem == key;
    })?.toList();
    if (list != null && list.length > 0) {
      PropertyHealthDataInfo info = list?.first;
      var value = 0.0;
      if (info.healthGrade <= 1) {
        value = setRatio(info.value / 10);
      } else {
        value = setRatio(info.illValue / 5);
      }
      Color textColor = userColor(info.healthGrade);
      String desc = '正常';
      switch(info.healthGrade) {
        case 0:
          desc = '正常';
          break;
        case 1:
          desc = '易生病';
          break;
        case 2:
        case 3:
          desc = '生病中';
          break;
      }
      RadarViewModel model = RadarViewModel(
        text: info.healthSystem+'\n($desc)',
        value: value,
        color: textColor,
      );
      return model;
    }
    return RadarViewModel(text: '', value: 0, color: Colors.white);
  }

  Color userColor(int healthGrade) {
    Color color;
    if (healthGrade <= 0) {
      color = Color.fromRGBO(132, 109, 255, 0.8);
    } else if (healthGrade == 1) {
      color = Color.fromRGBO(254, 173, 68, 0.8);
    } else if (healthGrade == 2 || healthGrade == 3) {
      color = Color.fromRGBO(255, 65, 92, 0.8);
    }
    return color;
  }
}
